package cn.lu.joke.adapter.decoration;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import cn.lu.joke.util.LogUtils;

public abstract class ItemDividerDecoration extends RecyclerView.ItemDecoration {

    private Paint mPaint;

    public ItemDividerDecoration() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        //left, top, right, bottom
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);

            int itemPosition = ((RecyclerView.LayoutParams) child.getLayoutParams()).getViewLayoutPosition();
            Divider divider = getDivider(itemPosition, childCount);

            if (divider != null) {
                if (divider.getLeftSideLine().isHave()) {
                    int lineWidthPx = divider.getLeftSideLine().getWidth();
                    int startPaddingPx = divider.getLeftSideLine().getStartPadding();
                    int endPaddingPx = divider.getLeftSideLine().getEndPadding();
                    drawChildLeftVertical(child, c, parent, divider.getLeftSideLine().getColor(), lineWidthPx, startPaddingPx, endPaddingPx);
                }
                if (divider.getTopSideLine().isHave()) {
                    int lineWidthPx = divider.getTopSideLine().getWidth();
                    int startPaddingPx = divider.getTopSideLine().getStartPadding();
                    int endPaddingPx = divider.getTopSideLine().getEndPadding();
                    drawChildTopHorizontal(child, c, parent, divider.getTopSideLine().getColor(), lineWidthPx, startPaddingPx, endPaddingPx);
                }
                if (divider.getRightSideLine().isHave()) {
                    int lineWidthPx = divider.getRightSideLine().getWidth();
                    int startPaddingPx = divider.getRightSideLine().getStartPadding();
                    int endPaddingPx = divider.getRightSideLine().getEndPadding();
                    drawChildRightVertical(child, c, parent, divider.getRightSideLine().getColor(), lineWidthPx, startPaddingPx, endPaddingPx);
                }
                if (divider.getBottomSideLine().isHave()) {
                    int lineWidthPx = divider.getBottomSideLine().getWidth();
                    int startPaddingPx = divider.getBottomSideLine().getStartPadding();
                    int endPaddingPx = divider.getBottomSideLine().getEndPadding();
                    drawChildBottomHorizontal(child, c, parent, divider.getBottomSideLine().getColor(), lineWidthPx, startPaddingPx, endPaddingPx);
                }
            }
        }
    }

    private void drawChildBottomHorizontal(View child, Canvas c, RecyclerView parent, @ColorInt int color, int lineWidth, int startPadding, int endPadding) {

        int leftPadding = startPadding - lineWidth;
        int rightPadding = lineWidth - endPadding;

        /*if (startPadding <= 0) {
            //padding<0当作==0处理
            //上下左右默认分割线的两头都出头一个分割线的宽度，避免十字交叉的时候，交叉点是空白
            leftPadding = -lineWidth;
        } else {
            leftPadding = startPadding;
        }

        if (endPadding <= 0) {
            rightPadding = lineWidth;
        } else {
            rightPadding = -endPadding;
        }*/

        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
        int left = child.getLeft() - params.leftMargin + leftPadding;
        int right = child.getRight() + params.rightMargin + rightPadding;
        int top = child.getBottom() + params.bottomMargin;
        int bottom = top + lineWidth;
        mPaint.setColor(color);

        c.drawRect(left, top, right, bottom, mPaint);

    }

    private void drawChildTopHorizontal(View child, Canvas c, RecyclerView parent, @ColorInt int color, int lineWidth, int startPadding, int endPadding) {
        int leftPadding = startPadding - lineWidth;
        int rightPadding = lineWidth - endPadding;

        /*if (startPadding <= 0) {
            //padding<0当作==0处理
            //上下左右默认分割线的两头都出头一个分割线的宽度，避免十字交叉的时候，交叉点是空白
            leftPadding = -lineWidth;
        } else {
            leftPadding = startPadding;
        }
        if (endPadding <= 0) {
            rightPadding = lineWidth;
        } else {
            rightPadding = -endPadding;
        }*/

        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
        int left = child.getLeft() - params.leftMargin + leftPadding;
        int right = child.getRight() + params.rightMargin + rightPadding;
        int bottom = child.getTop() - params.topMargin;
        int top = bottom - lineWidth;
        mPaint.setColor(color);

        c.drawRect(left, top, right, bottom, mPaint);

    }

    private void drawChildLeftVertical(View child, Canvas c, RecyclerView parent, @ColorInt int color, int lineWidth, int startPadding, int endPadding) {
        int topPadding = startPadding - lineWidth;
        int bottomPadding = lineWidth - endPadding;

       /* if (startPaddingPx <= 0) {
            //padding<0当作==0处理
            //上下左右默认分割线的两头都出头一个分割线的宽度，避免十字交叉的时候，交叉点是空白
            topPadding = -lineWidthPx;
        } else {
            topPadding = startPaddingPx;
        }
        if (endPaddingPx <= 0) {
            bottomPadding = lineWidthPx;
        } else {
            bottomPadding = -endPaddingPx;
        }*/

        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
        int top = child.getTop() - params.topMargin + topPadding;
        int bottom = child.getBottom() + params.bottomMargin + bottomPadding;
        int right = child.getLeft() - params.leftMargin;
        int left = right - lineWidth;
        mPaint.setColor(color);

        c.drawRect(left, top, right, bottom, mPaint);

    }

    private void drawChildRightVertical(View child, Canvas c, RecyclerView parent, @ColorInt int color, int lineWidth, int startPadding, int endPadding) {

        int topPadding = startPadding;
        int bottomPadding = endPadding;

       /* if (startPaddingPx <= 0) {
            //padding<0当作==0处理
            //上下左右默认分割线的两头都出头一个分割线的宽度，避免十字交叉的时候，交叉点是空白
            //topPadding = -lineWidthPx;
        } else {
            topPadding = startPaddingPx;
        }
        if (endPaddingPx <= 0) {
            //bottomPadding = lineWidthPx;
        } else {
            bottomPadding = -endPaddingPx;
        }*/

       /*if (startPadding > 0) {
           topPadding = startPadding;
       }
       if (endPadding > 0) {
           bottomPadding = endPadding;
       }*/

        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
        int top = child.getTop() - params.topMargin + topPadding;
        int bottom = child.getBottom() + params.bottomMargin - bottomPadding;
        int left = child.getRight() + params.rightMargin;
        int right = left + lineWidth;
        mPaint.setColor(color);

        c.drawRect(left, top, right, bottom, mPaint);

    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

        //outRect 只是把Rect类型的outRect作为一个封装了left,right,top,bottom的数据结构,
        //作为传递left,right,top,bottom的偏移值来用的

        int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
        int itemCount = state.getItemCount();

        Divider divider = getDivider(itemPosition, itemCount);

        if (divider != null) {
            int left = divider.getLeftSideLine().getWidth();
            int top = divider.getTopSideLine().getWidth();
            int right = divider.getRightSideLine().getWidth();
            int bottom = divider.getBottomSideLine().getWidth();

            outRect.set(left, top, right, bottom);
        }

    }


    public abstract Divider getDivider(int itemPosition, int itemCount);


}

















